package cn.explink.dop.controller;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.explink.dop.entity.vo.OrderStatusRecordVO;
import cn.explink.dop.entity.vo.OrderVO;
import cn.explink.dop.enums.OrderStatusEnum;
import cn.explink.dop.service.OrderService;
import cn.explink.dop.service.OrderStatusRecordService;
import cn.explink.eap.core.common.model.json.AjaxJson;
import cn.explink.eap.web.cgform.controller.autolist.CgAutoListController;

/**
 *
 * @ClassName: StorefrontAccept
 * @Description: 门店受理订单
 * @author 孟凡岭
 * @date 2015年4月21日 下午4:27:26
 * @since AR1.0
 */
@Controller
@RequestMapping("/storefrontAcceptController")
public class StorefrontAcceptController extends CgAutoListController {
	/**
	 * 订单Service
	 */
	@Autowired
	private OrderService orderService;
	/**
	 * 订单状态记录Service
	 */
	@Autowired
	private OrderStatusRecordService orderStatusRecordService;

	/**
	 *
	 * @Title: index
	 * @Description: 页面进入跳转
	 * @author 孟凡岭
	 * @param @param request
	 * @param @return
	 * @return ModelAndView 返回类型
	 * @throws
	 */
	@RequestMapping(params = "index")
	public ModelAndView index(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dop/storefrontAccept/storefrontAcceptList");
		return view;
	}

	/**
	 *
	 * @Title: getOrderStatus
	 * @Description: 获取订单状态
	 * @author 孟凡岭
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "getOrderStatus")
	@ResponseBody
	public AjaxJson getOrderStatus(HttpServletRequest request) {
		AjaxJson ajaxJson = new AjaxJson();
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		List<OrderStatusEnum> list = OrderStatusEnum.getAllStatus();
		map.put("0", "全部");
		for (int i = 0; i < list.size(); i++) {
			OrderStatusEnum enmu = list.get(i);
			map.put(enmu.getValue() + "", enmu.getText());
		}
		ajaxJson.setAttributes(map);
		return ajaxJson;
	}

	/**
	 *
	 * @Title: queryData
	 * @Description: 店面受理查询订单
	 * @author 孟凡岭
	 * @param @param orderVO
	 * @param @param pageNum
	 * @param @param pageSize
	 * @param @param request
	 * @param @return
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	@RequestMapping(params = "query")
	@ResponseBody
	public Map<String, Object> queryData(OrderVO orderVO, int pageNum, int pageSize, HttpServletRequest request) {
		try {
			return this.orderService.storeAcceptGetOrderList(orderVO, pageNum, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			return new HashMap<String, Object>();
		}
	}

	/**
	 *
	 * @Title: accept
	 * @Description: 门店接受订单
	 * @author 孟凡岭
	 * @param @param ids
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "accept")
	@ResponseBody
	public AjaxJson accept(@RequestParam(value = "ids[]", required = false) String[] ids, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			this.orderService.storeFrontAccept(ids);
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j;

	}

	/**
	 *
	 * @Title: loadRecord
	 * @Description: 加载订单状态记录信息
	 * @author 孟凡岭
	 * @param @param orderId
	 * @param @param request
	 * @param @return
	 * @return List<OrderStatusRecordVO> 返回类型
	 * @throws
	 */
	@RequestMapping(params = "loadRecord")
	@ResponseBody
	public List<OrderStatusRecordVO> loadRecord(String orderId, HttpServletRequest request) {
		return this.orderStatusRecordService.queryByOrderId(orderId);
	}

	/**
	 *
	 * @Title: delive
	 * @Description: 分配派送人员，并返回记录数
	 * @author 孟凡岭
	 * @param @param ids
	 * @param @param manId
	 * @param @param request
	 * @param @return
	 * @return Map<String,Integer> 返回类型
	 * @throws
	 */
	@RequestMapping(params = "delive")
	@ResponseBody
	public Map<String, Integer> delive(@RequestParam(value = "ids[]", required = false) String[] ids, String manId, HttpServletRequest request) {
		return this.orderService.delive(ids, manId);
	}

	/**
	 *
	 * @Title: cancelOrder
	 * @Description: 订单取消
	 * @author 孟凡岭
	 * @param @param ids
	 * @param @param outCauseDesc
	 * @param @param insideCauseDesc
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "cancelOrder")
	@ResponseBody
	public AjaxJson cancelOrder(@RequestParam(value = "arr[]", required = true) String[] ids, String outCauseDesc, String insideCauseDesc, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			Map<String, Object> map = this.orderService.cancelOrder(ids, outCauseDesc, insideCauseDesc);
			j.setSuccess(true);
			j.setObj(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j;
	}

	/**
	 *
	 * @Title: startMakeFood
	 * @Description: 开始备单
	 * @author 孟凡岭
	 * @param @param ids
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "startMakeFood")
	@ResponseBody
	public AjaxJson startMakeFood(@RequestParam(value = "arr[]", required = true) String[] ids, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			Map<String, Object> map = this.orderService.startMakeFood(ids);
			j.setSuccess(true);
			j.setObj(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j;
	}

	/**
	 *
	 * @Title: makeFoodSuccess
	 * @Description: 订单备单完成
	 * @author 孟凡岭
	 * @param @param ids
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "makeFoodSuccess")
	@ResponseBody
	public AjaxJson makeFoodSuccess(@RequestParam(value = "arr[]", required = true) String[] ids, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			Map<String, Object> map = this.orderService.makeFoodSuccess(ids);
			j.setSuccess(true);
			j.setObj(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j;
	}

}
